package com.whfc.emp.realNamePlatform.jingZhou;

import com.alibaba.fastjson.JSONObject;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.HttpUtil;
import com.whfc.common.util.ImageUtil;
import com.whfc.emp.entity.*;
import com.whfc.emp.realNamePlatform.jingZhou.model.ProjectWorkerDTO;
import com.whfc.emp.realNamePlatform.jingZhou.model.TeamDTO;
import com.whfc.emp.realNamePlatform.jingZhou.model.WorkerAttendDTO;
import com.whfc.emp.realNamePlatform.jingZhou.model.WorkerEntryExitDTO;
import com.whfc.emp.realNamePlatform.jingZhou.util.AesUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Slf4j
public  class JINGZHOUApi {

    // 查询可以对接的所有工地码
    public final static String QUERY_GDM_CODE = "queryGdmCode";
    // 查询工程及参建方信息
    public final static String GET_ENGINFO = "getEngInfo";
    // 查询工程的关键岗位人员信息
    public final static String GET_ENG_KEY_JOB = "getEngKeyjob";
    // 上传班组
    public final static String TEAM_UPLOAD = "teamUpload";
    // 修改班组
    public final static String TEAM_UPDATE = "teamUpdate";
    // 删除班组
    public final static String TEAM_DELETE = "teamDelete";
    // 上传项目工人信息
    public final static String PROJECT_WORKER_UPLOAD = "projectWorkerUpload";
    // 修改项目工人信息
    public final static String PROJECT_WORKER_UPDATE = "projectWorkerUpdate";
    // 删除工人信息
    public final static String PROJECT_WORKER_DELETE = "projectWorkerDelete";
    // 上传项目工人进/退场信息
    public final static String WORKER_ENTRYEXIT_UPLOAD = "workerEntryExitUpload";
    // 上传工人考勤
    public final static String WORKER_ATTEN_UPLOAD = "workerAttendanceUpload";


    @Test
    public void queryGdmCode(){
        // 测试
        AppSyncRealNameConfig test = new AppSyncRealNameConfig();
        test.setAppId("130148771239448da31d0ee218765a00");
        test.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        test.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");


        // 生产
        AppSyncRealNameConfig pro = new AppSyncRealNameConfig();
        pro.setAppId("bb2e5607f8fc44199805deb5338adbb4");
        pro.setAppSecret("31daba5dad5a46bc958e05ec8833fc6a");
        pro.setPushUrl("https://www.jzzhzj.com/laborjz/open/api/v1/");
        queryGdmCode(pro);
    }

    /**
     * queryGdmCode
     * @param appSyncRealNameConfig
     */
    public static void queryGdmCode(AppSyncRealNameConfig appSyncRealNameConfig){
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();
        String method = QUERY_GDM_CODE;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("appid",appid);
        log.info("荆州实名制查询工程及参建方信息appid={},appsecret={},jsonObject={}",appid,appsecret,jsonObject);
        JINGZHOUApi.doPost(appid,appsecret,method,url,jsonObject);
    }

    @Test
    public  void testGetEngInfo(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("bb2e5607f8fc44199805deb5338adbb4");
        appSyncRealNameConfig.setAppSecret("31daba5dad5a46bc958e05ec8833fc6a");
        appSyncRealNameConfig.setGdmCode("81a60f24755b326a6fabba914f7038a9");
        appSyncRealNameConfig.setPushUrl("https://www.jzzhzj.com/laborjz/open/api/v1/");
        getEngInfo(appSyncRealNameConfig);
    }

    public static JSONObject getEngInfo(AppSyncRealNameConfig appSyncRealNameConfig){
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();
        String method = GET_ENGINFO;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("gdmCode",appSyncRealNameConfig.getGdmCode());
        log.info("荆州实名制查询工程及参建方信息appid={},appsecret={},jsonObject={}",appid,appsecret,jsonObject);
        return JINGZHOUApi.doPost(appid,appsecret,method,url,jsonObject);
    }


    @Test
    public  void testSyncTeam(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        String method = TEAM_UPLOAD;
        AppEmpGroup appEmpGroup = new AppEmpGroup();
        appEmpGroup.setCorpCode("12421000691799738H");
        appEmpGroup.setGroupCode("test-team-0001");
        appEmpGroup.setGroupName("测试班组001");
        appEmpGroup.setResponsiblePersonName("领导1");
        appEmpGroup.setResponsiblePersonPhone("15072016663");
        syncTeam(appEmpGroup,appSyncRealNameConfig,method);
    }

    //  新增 修改班组
    public static JSONObject syncTeam(AppEmpGroup appEmpGroup,AppSyncRealNameConfig appSyncRealNameConfig,String method)throws BizException{
            // 对接荆州实名制
            String appid = appSyncRealNameConfig.getAppId();
            String appsecret = appSyncRealNameConfig.getAppSecret();
            String url = appSyncRealNameConfig.getPushUrl();
            TeamDTO teamDTO =new TeamDTO();
            teamDTO.setGdmCode(appSyncRealNameConfig.getGdmCode());
            teamDTO.setProjectCode(appSyncRealNameConfig.getProjectCode());
            teamDTO.setCorpCode(appEmpGroup.getCorpCode());
            teamDTO.setTeamSysNo(appEmpGroup.getGroupCode());
            teamDTO.setTeamName(appEmpGroup.getGroupName());
            teamDTO.setWorkType(StringUtils.hasText(appEmpGroup.getWorkTypeName())?appEmpGroup.getWorkTypeName():"未知");
            teamDTO.setTeamLeaderName(appEmpGroup.getResponsiblePersonName());
            teamDTO.setTeamLeaderIDNumber(appEmpGroup.getResponsiblePersonPhone());
            log.info("同步荆州实名制班组信息appid={},appsecret={},method={},teamDTO={}",appid,appsecret,method,teamDTO.toString());
            return JINGZHOUApi.doPost(appid,appsecret,method,url,(JSONObject) JSONObject.toJSON(teamDTO));
    }

    @Test
    public  void testSyncDelTeam(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        AppEmpGroup appEmpGroup = new AppEmpGroup();
        appEmpGroup.setGroupCode("test-team-0001");
        syncDelTeam(appEmpGroup,appSyncRealNameConfig);
    }
    // 删除分组
    public static JSONObject syncDelTeam(AppEmpGroup appEmpGroup,AppSyncRealNameConfig appSyncRealNameConfig)throws BizException{
        String groupCode = appEmpGroup.getGroupCode();
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("gdmCode",appSyncRealNameConfig.getGdmCode());
        jsonObject.put("teamSysNo",groupCode);
        log.info("同步荆州实名制人员进出场信息appid={},appsecret={},jsonObject={}",appid,appsecret,jsonObject);
        return JINGZHOUApi.doPost(appid,appsecret,JINGZHOUApi.TEAM_DELETE,url,jsonObject);
    }

    /**
     * 新增、编辑人员
     * @param appEmp
     * @param avatarBase64
     * @param empEnterRecord
     * @param appEmpGroup
     * @param appSyncRealNameConfig
     * @param method
     */
    public static JSONObject syncWorkerAddOrUpdate(AppEmp appEmp, String avatarBase64, AppEmpEnterRecord empEnterRecord, AppEmpGroup appEmpGroup, AppSyncRealNameConfig appSyncRealNameConfig, String method) throws BizException{
        // 对接荆州实名制
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();

        ProjectWorkerDTO workerDTO = new ProjectWorkerDTO();
        workerDTO.setGdmCode(appSyncRealNameConfig.getGdmCode());
        workerDTO.setCorpCode(appEmpGroup.getCorpCode());
        workerDTO.setTeamSysNo(appEmpGroup.getGroupCode());
        workerDTO.setWorkerName(appEmp.getEmpName());
        workerDTO.setIdCardType("1");
        workerDTO.setIdCardNumber(AesUtils.encrypt(appEmp.getIdCardNo(), appsecret));
        workerDTO.setHeadImage(avatarBase64==null?"无":avatarBase64);
        workerDTO.setIsTeamLeader(appEmp.getLeaderFlag() == 1 ? "1" : "2");
        workerDTO.setWorkType(appEmp.getWorkTypeName());
        workerDTO.setNation(appEmp.getNation() == null ? "暂无" : appEmp.getNation());
        workerDTO.setAddress(appEmp.getAddress() == null ? "未知" : appEmp.getAddress());
        workerDTO.setCellPhone(appEmp.getPhone());
        workerDTO.setPoliticsType("13");
        workerDTO.setCultureLevelType("9");
        workerDTO.setGrantOrg(appEmp.getIdCardGrantOrg());
        workerDTO.setStartDate(appEmp.getIdCardStartDate());
        workerDTO.setExpiryDate(appEmp.getIdCardEndDate());
        workerDTO.setEntryTime(empEnterRecord.getDate());

        // 新增上传
        if (JINGZHOUApi.PROJECT_WORKER_UPLOAD.equals(method)) {
            List<ProjectWorkerDTO> list = new ArrayList<>();
            list.add(workerDTO);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("gdmCode",workerDTO.getGdmCode());
            jsonObject.put("corpCode",workerDTO.getCorpCode());
            jsonObject.put("teamSysNo",workerDTO.getTeamSysNo());
            jsonObject.put("workerList",list);
            log.info("同步荆州实名制人员新增编辑信息appid={},appsecret={},method={},jsonObject={}",appid,appsecret,method,jsonObject);
            JSONObject result = JINGZHOUApi.doPost(appid, appsecret, method, url, jsonObject);
            if (0==result.getInteger("code")) {
                // 人员进场============
                 syncWorkerEnter(appEmp.getIdCardNo(),appEmpGroup.getGroupCode(),appSyncRealNameConfig,"1");
            }
            return result;
        }
        if (JINGZHOUApi.PROJECT_WORKER_UPDATE.equals(method)) {
            log.info("同步荆州实名制人员新增编辑信息appid={},appsecret={},method={},workerDTO={}",appid,appsecret,method,workerDTO.toString());
            return JINGZHOUApi.doPost(appid, appsecret, method, url, (JSONObject) JSONObject.toJSON(workerDTO));
        }
        return new JSONObject();
    }

    @Test
    public void testSyncWorker(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        AppEmp appEmp = new AppEmp();
        appEmp.setEmpName("测试人员2");
        appEmp.setIdCardNo("420281199304052137");
        appEmp.setIdCardGrantOrg("黄石市公安局");
        appEmp.setIdCardStartDate(DateUtil.addDays(new Date(),-100));
        appEmp.setIdCardEndDate(new Date());
        appEmp.setPhone("15072013437");
        appEmp.setLeaderFlag(0);
        appEmp.setWorkTypeName("瓦工");

        AppEmpEnterRecord empEnterRecord = new AppEmpEnterRecord();
        empEnterRecord.setDate(DateUtil.addDays(new Date(),-4));

        AppEmpGroup appEmpGroup = new AppEmpGroup();
        appEmpGroup.setCorpCode("12421000691799738H");
        appEmpGroup.setGroupCode("test-team-0002");
        syncWorkerAddOrUpdate(appEmp, ImageUtil.base64EncodeAndCompress("https://file.gbuvip.com/file/operationPic/202302182021214517.jpg",40),empEnterRecord,appEmpGroup,appSyncRealNameConfig,JINGZHOUApi.PROJECT_WORKER_UPLOAD);
    }
    /**
     *人员进出场
     * @param
     * @param appSyncRealNameConfig
     * @param type 类型(1:进场,0:退场)
     */
    public static JSONObject syncWorkerEnter(String idCardNo,String groupCode,AppSyncRealNameConfig appSyncRealNameConfig,String type)throws BizException{
        // 对接荆州实名制
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();
        // 人员进场============
        WorkerEntryExitDTO workerEntryExitDTO = new WorkerEntryExitDTO();
        workerEntryExitDTO.setGdmCode(appSyncRealNameConfig.getGdmCode());
        workerEntryExitDTO.setTeamSysNo(groupCode);

        WorkerEntryExitDTO.WorkerDTO workerDTO1 = new WorkerEntryExitDTO.WorkerDTO();
        workerDTO1.setIdCardNumber(AesUtils.encrypt(idCardNo, appsecret));
        workerDTO1.setDate(DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
        workerDTO1.setType(type);

        List<WorkerEntryExitDTO.WorkerDTO> workerList = new ArrayList<>();
        workerList.add(workerDTO1);
        workerEntryExitDTO.setWorkerList(workerList);
        log.info("同步荆州实名制人员进出场信息appid={},appsecret={},workerEntryExitDTO={}",appid,appsecret,workerEntryExitDTO.toString());
        return JINGZHOUApi.doPost(appid, appsecret, JINGZHOUApi.WORKER_ENTRYEXIT_UPLOAD, url, (JSONObject) JSONObject.toJSON(workerEntryExitDTO));

    }

    @Test
    public  void syncWorkerEnter(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        syncWorkerEnter("420281199304052136","test-team-0001",appSyncRealNameConfig,"1");

    }
    /**
     * 删除人员
     * @param idCardNo
     * @param groupCode
     * @param appSyncRealNameConfig
     */
    public static JSONObject syncDelWorker(String idCardNo,String groupCode,AppSyncRealNameConfig appSyncRealNameConfig)throws BizException{
        // 对接荆州实名制
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("gdmCode", appSyncRealNameConfig.getGdmCode());
        jsonObject.put("teamSysNo", groupCode);
        jsonObject.put("idCardNumber", AesUtils.encrypt(idCardNo, appsecret));
        log.info("同步荆州实名制人员删除信息appid={},appsecret={},jsonObject={}",appid,appsecret,jsonObject);
        return JINGZHOUApi.doPost(appid, appsecret, JINGZHOUApi.PROJECT_WORKER_DELETE, url, jsonObject);
    }

    @Test
    public  void testSyncDelWorker(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        syncDelWorker("420281199304052136","test-team-0001",appSyncRealNameConfig);

    }
    /**
     * 同步考勤
     * @param
     * @param
     * @param appSyncRealNameConfig
     */
    public static JSONObject syncAtte(String imhBase64,String idCard,String groupCode, AppSyncRealNameConfig appSyncRealNameConfig, AppEmpAttendRecord record)throws BizException{

        // 对接荆州实名制
        String appid = appSyncRealNameConfig.getAppId();
        String appsecret = appSyncRealNameConfig.getAppSecret();
        String url = appSyncRealNameConfig.getPushUrl();

        WorkerAttendDTO workerAttendDTO = new  WorkerAttendDTO();
        workerAttendDTO.setGdmCode(appSyncRealNameConfig.getGdmCode());
        workerAttendDTO.setTeamSysNo(groupCode);
        List<WorkerAttendDTO.AttendDTO> list = new ArrayList<>();
        WorkerAttendDTO.AttendDTO attendDTO = new WorkerAttendDTO.AttendDTO();
        attendDTO.setIdCardNumber(AesUtils.encrypt(idCard, appsecret));
        attendDTO.setDate(DateUtil.formatDate(record.getTime(),"yyyy-MM-dd HH:mm:ss"));
        attendDTO.setDirection("1".equals(String.valueOf(record.getDirection()))?"1":"2");
        attendDTO.setAttendType(imhBase64==null?"2":"1");
        attendDTO.setImage(imhBase64==null?"":imhBase64);
        list.add(attendDTO);
        workerAttendDTO.setDataList(list);
        log.info("同步荆州实名制考勤信息appid={},appsecret={}",appid,appsecret);
        return JINGZHOUApi.doPost(appid, appsecret, JINGZHOUApi.WORKER_ATTEN_UPLOAD, url, (com.alibaba.fastjson.JSONObject) com.alibaba.fastjson.JSONObject.toJSON(workerAttendDTO));
    }

    @Test
    public void testSyncAtte(){
        AppSyncRealNameConfig appSyncRealNameConfig = new AppSyncRealNameConfig();
        appSyncRealNameConfig.setAppId("130148771239448da31d0ee218765a00");
        appSyncRealNameConfig.setAppSecret("d5354df148be4766ae6a8135cd356e2e");
        appSyncRealNameConfig.setGdmCode("406afaf85fe524a97d41b93d32dbeb23");
        appSyncRealNameConfig.setProjectCode("f9c42f7c58bb4e4db8a25c8e0c0593e2");
        appSyncRealNameConfig.setPushUrl("http://111.47.65.236:2229/laborjz/open/api/v1/");
        AppEmpAttendRecord record = new AppEmpAttendRecord();
        record.setTime(new Date());
        record.setDirection(1);
        syncAtte(ImageUtil.base64EncodeAndCompress("https://file.gbuvip.com/file/operationPic/202302182021214517.jpg",40),"420281199304052136","test-team-0001",appSyncRealNameConfig,record);
    }



    public static JSONObject doPost(String appid,String appsecret,String method,String url,JSONObject jsonObject) throws BizException{
        String format = "json";
        String nonce = UUID.randomUUID().toString();
        String timestamp = DateUtil.formatDate(new Date(), "yyyyMMddHHmmss");
        String version = "v1.1";
        String data = jsonObject.toJSONString();
        // 签名
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("appid=" + appid);
        sBuffer.append("&data=" + data);
        sBuffer.append("&format=" + format);
        sBuffer.append("&method=" + method);
        sBuffer.append("&nonce=" + nonce);
        sBuffer.append("&timestamp=" + timestamp);
        sBuffer.append("&version=" + version);
        sBuffer.append("&appsecret=" + appsecret);
        String signStr = getSHA256(sBuffer.toString().toLowerCase());
        log.info("=====生成签名={}",signStr);
        // 设置固定参数
        jsonObject.put("appid",appid);
        jsonObject.put("format",format);
        jsonObject.put("method",method);
        jsonObject.put("nonce",nonce);
        jsonObject.put("timestamp",timestamp);
        jsonObject.put("version",version);
        jsonObject.put("appsecret",appsecret);
        jsonObject.put("data",data);
        jsonObject.put("sign",signStr);

        String result = HttpUtil.doPost(url,JSONObject.toJSONString(jsonObject));
        JSONObject jsonData = JSONObject.parseObject(result);
        log.info("调用结果：{}",jsonData);
        if (0!=jsonData.getInteger("code")) {
            throw new BizException(ResultEnum.UNKNOWN_ERROR.getCode(),jsonData.getString("msg"));
        }
        return jsonData;
    }

    @Test
    public void testSign(){
        String appid = "bb2e5607f8fc44199805deb5338adbb4";
        String data = "{\"appid\":\"bb2e5607f8fc44199805deb5338adbb4\"}";
        String format = "json";
        String method = "queryGdmCode";
        String nonce = "48e89f3c-83a3-4070-a728-4b51c1cfdb44";
        String timestamp = "20230426145540";
        String version = "v1.1";
        String appsecret = "31daba5dad5a46bc958e05ec8833fc6a";

        // 签名
        StringBuffer sBuffer = new StringBuffer();
        sBuffer.append("appid=" + appid);
        sBuffer.append("&data=" + data);
        sBuffer.append("&format=" + format);
        sBuffer.append("&method=" + method);
        sBuffer.append("&nonce=" + nonce);
        sBuffer.append("&timestamp=" + timestamp);
        sBuffer.append("&version=" + version);
        sBuffer.append("&appsecret=" + appsecret);
        String signStr = getSHA256(sBuffer.toString().toLowerCase());
        System.out.println(signStr);
    }


    public static String getSHA256(String content) {
        try {
            // Create MD5 Hash
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(content.getBytes());
            byte[] messageDigest = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < messageDigest.length; i++)
                hexString.append(String.format("%02X", 0xFF &
                        messageDigest[i]));
            return hexString.toString().toLowerCase();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

}
